package com.hoperun.product.wgyutil;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import org.json.JSONObject;

import java.util.Iterator;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wgy on 2018/12/12.
 */

public class WgyUtil {

     public static JSONObject transToLowerObject(JSONObject jSONArray1) {
         try {
             JSONObject jSONArray2 = new JSONObject();
             Iterator it = jSONArray1.keys();
             while (it.hasNext()) {
                 String key = (String) it.next();
                 Object object = jSONArray1.get(key);
                 if (object.getClass().toString().endsWith("String")) {
                     jSONArray2.accumulate(key.toLowerCase(), object);
                 } else if (object.getClass().toString().endsWith("JSONObject")) {
                     jSONArray2.accumulate(key.toLowerCase(), transToLowerObject((JSONObject) object));
                 }
//                 else if (object.getClass().toString().endsWith("JSONArray")) {
//                     jSONArray2.accumulate(key.toLowerCase(), transToArray(jSONArray1.getJSONArray(key)));
//                 }
            }
             return jSONArray2;
         }catch (Exception e){

         }
         return null;
     }

    public static String getUUID(){
        String uuid = UUID.randomUUID().toString();
        //去掉“-”符号
        return uuid.replaceAll("-", "");
    }

    public static boolean isOnlyPointNumber(String number) {//保留两位小数正则
        Pattern pattern = Pattern.compile("^\\d+\\.?\\d{0,2}$");
        Matcher matcher = pattern.matcher(number);
        return matcher.matches();
    }


    public static boolean isOnlyonePointNumber(String number) {//保留1位小数正则
        Pattern pattern = Pattern.compile("^\\d+\\.?\\d{0,1}$");
        Matcher matcher = pattern.matcher(number);
        return matcher.matches();
    }

    public static int getVersionCode(Context context){
        PackageManager packageManager=context.getPackageManager();
        PackageInfo packageInfo;
        int versionCode;
        try {
            packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionCode=packageInfo.versionCode;
            return versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }

    }

    public static String getVersion(Context context) {
        try {
            PackageManager packageManager=context.getPackageManager();
            PackageInfo info = packageManager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "无法获取到版本号";
        }
    }


}
